package br.uvv.Util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Encoder;

public class CriptoHashUtil {
	
	/* Recebe a string que queremos aplicar o Hash
	 * E recebe o algorítimo ('SHA1' ou 'MD5') 
	 * */
	public static String hash(String algoritimo, String str){
		try {
			MessageDigest digest = MessageDigest.getInstance(algoritimo);
			digest.update(str.getBytes());
			BASE64Encoder encoder = new BASE64Encoder ();
			str = encoder.encode (digest.digest());
			return str;
		} catch (NoSuchAlgorithmException e) {
			System.out.println("ERRO: Ao aplicar o hash!");
			e.printStackTrace();
		}
		return null;
	}
}
